import mongoose from 'mongoose';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';

// 配置环境变量
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config({ path: path.resolve(__dirname, '.env') });

// 测试MongoDB连接
const testMongoDBConnection = async () => {
  try {
    console.log('正在连接MongoDB...');
    await mongoose.connect(process.env.MONGODB_URI, {
      serverSelectionTimeoutMS: 5000 // 5秒超时
    });
    console.log('✅ MongoDB 连接成功！');
    return true;
  } catch (err) {
    console.error('❌ MongoDB 连接失败:', err.message);
    console.error('提示: 请确保MongoDB服务已启动，或检查.env文件中的连接字符串配置是否正确。');
    return false;
  }
};

// 测试模型导入
const testModelImports = async () => {
  try {
    console.log('\n正在测试模型导入...');
    
    // 测试Clothes模型导入
    const Clothes = await import('./backend/models/Clothes.js');
    console.log('✅ Clothes模型导入成功！');
    
    // 测试Outfit模型导入
    const Outfit = await import('./backend/models/Outfit.js');
    console.log('✅ Outfit模型导入成功！');
    
    // 测试User模型导入
    const User = await import('./backend/models/User.js');
    console.log('✅ User模型导入成功！');
    
    return true;
  } catch (err) {
    console.error('❌ 模型导入失败:', err.message);
    return false;
  }
};

// 测试控制器导入
const testControllerImports = async () => {
  try {
    console.log('\n正在测试控制器导入...');
    
    // 测试clothesController导入
    const clothesController = await import('./backend/controllers/clothesController.js');
    console.log('✅ clothesController导入成功！包含以下函数:', Object.keys(clothesController));
    
    // 测试outfitController导入
    const outfitController = await import('./backend/controllers/outfitController.js');
    console.log('✅ outfitController导入成功！包含以下函数:', Object.keys(outfitController));
    
    return true;
  } catch (err) {
    console.error('❌ 控制器导入失败:', err.message);
    return false;
  }
};

// 测试路由导入
const testRouteImports = async () => {
  try {
    console.log('\n正在测试路由导入...');
    
    // 测试clothesRoutes导入
    const clothesRoutes = await import('./backend/routes/clothesRoutes.js');
    console.log('✅ clothesRoutes导入成功！');
    
    // 测试outfitRoutes导入
    const outfitRoutes = await import('./backend/routes/outfitRoutes.js');
    console.log('✅ outfitRoutes导入成功！');
    
    return true;
  } catch (err) {
    console.error('❌ 路由导入失败:', err.message);
    return false;
  }
};

// 主测试函数
const runTests = async () => {
  console.log('==========================');
  console.log('    衣橱管理系统修复测试    ');
  console.log('==========================');
  
  const mongoResult = await testMongoDBConnection();
  const modelResult = await testModelImports();
  const controllerResult = await testControllerImports();
  const routeResult = await testRouteImports();
  
  console.log('\n==========================');
  console.log('        测试结果总结       ');
  console.log('==========================');
  
  const allPassed = mongoResult && modelResult && controllerResult && routeResult;
  
  if (allPassed) {
    console.log('✅ 所有测试通过！代码修复成功！');
    console.log('\n下一步建议:');
    console.log('1. 运行 `npm run server` 启动后端服务器');
    console.log('2. 运行 `npm run dev` 启动前端开发服务器');
    console.log('3. 访问 http://localhost:5173 测试应用功能');
  } else {
    console.log('❌ 部分测试未通过，请查看详细错误信息进行修复。');
  }
  
  // 断开MongoDB连接
  if (mongoose.connection.readyState === 1) {
    await mongoose.disconnect();
    console.log('\nMongoDB连接已断开。');
  }
};

// 运行测试
runTests().catch(err => {
  console.error('测试执行过程中发生错误:', err);
});